/**
 * 
 */
package dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import entries.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author zharikhin
 *
 */
public class JdbcRoleDao extends AbstractJdbcDao implements RoleDao {

    private final static Log LOGGER = LogFactory.getLog(JdbcRoleDao.class);

    Connection conn;
    ResultSet result;
    PreparedStatement prepSt;
    private String property = "properties.xml";

    public JdbcRoleDao() {

        propertyFile = property;

        try {
            props.loadFromXML(this.getClass().getClassLoader().getResourceAsStream(propertyFile));
            driverName = props.getProperty("driverName");
            url = props.getProperty("url");
            login = props.getProperty("login");
            password = props.getProperty("password");
        } catch (IOException e) {
            LOGGER.error("IOException", e);
        }
    }
    
    public JdbcRoleDao (String propertyFile){
        super(propertyFile);
    }

    public void create(Role role) {

        try{
            conn = createConnection();
            PreparedStatement prst = null;
            prst = conn.prepareStatement("insert into ROLE (ROLE_ID, ROLE_NAME)" +
                    " values (?,?)");
            prst.setLong(1, role.getId());
            prst.setString(2, role.getName());
            prst.execute();
        }
        catch(SQLException e){
            LOGGER.error("SQLException", e);
        }
        finally{
            if (prepSt != null){
                try {
                    prepSt.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            } 
            if (conn != null){
                try {
                    conn.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }
        }
    }

    public void update(Role role) {

        try{
            conn = createConnection();

            prepSt = conn.prepareStatement("update ROLE SET ROLE_NAME=?" +
                    "where ROLE_ID = ?");
            prepSt.setString(1, role.getName());
            prepSt.setLong(2, role.getId());
            prepSt.execute();
        }
        catch(SQLException e){
            LOGGER.error("SQLException", e);
        }
        finally{
            if (prepSt != null){
                try {
                    prepSt.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            } 
            if (conn != null){
                try {
                    conn.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }
        }
    }

    public void remove(Role role) {
        
        try{
            conn = createConnection();
            prepSt = conn.prepareStatement("delete from ROLE " +
                    "where ROLE_ID = ?");
            prepSt.setLong(1, role.getId());
            prepSt.execute();
        }
        catch(SQLException e){
            LOGGER.error("SQLException", e);
        }
        finally{
            if (prepSt != null){
                try {
                    prepSt.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            } 
            if (conn != null){
                try {
                    conn.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }
        }
        
    }

    public Role findByName(String name) {
        
        Role role = new Role();

        try{            
            conn = createConnection();
            prepSt = conn.prepareStatement("select * from ROLE where ROLE_NAME=?");
            prepSt.setString(1, name);
            result = prepSt.executeQuery();
            
            while (result.next()) {
                role.setId(Long.parseLong(result.getString("ROLE_ID")));
                role.setName(result.getString("ROLE_NAME"));
            }
        }
        catch(SQLException e) {
            LOGGER.error("SQLException", e);
        }
        finally {
            if (prepSt != null) {
                try {
                    prepSt.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            } 
            if (conn != null ){
                try {
                    conn.close();
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }
        }
        return role;        
    }
}
